package com.zhixiang.xaec.action.bpmndemo;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zhixiang.core.command.QueryFilter;
import com.zhixiang.core.util.String2Array;
import com.zhixiang.core.web.action.BaseAction;
import com.zhixiang.xaec.entity.bpmndemo.WorkflowConfig;
import com.zhixiang.xaec.service.bpmndemo.WorkflowConfigService;

/**
 * @author gao
 * @since 2017年1月11日
 * 
 */
@Controller
@Scope("prototype")
public class WorkflowConfigAction extends BaseAction
{

    private static final long serialVersionUID = 1L;
    
    private WorkflowConfig workflowConfig;
    
    
    public WorkflowConfig getWorkflowConfig()
    {
        return workflowConfig;
    }

    public void setWorkflowConfig(WorkflowConfig workflowConfig)
    {
        this.workflowConfig = workflowConfig;
    }
    
    @Autowired
    private WorkflowConfigService workflowConfigService;

       public String list(){
            QueryFilter localQueryFilter=new QueryFilter(getRequest());
            List <WorkflowConfig>list= workflowConfigService.getAll(localQueryFilter);
            setTotalCounts(localQueryFilter.getPagingBean().getTotalItems());
            setResult(list);
            return SUCCESS;
        }
        
        public String get(){
            String id=getRequest().getParameter("id");
            if(StringUtils.isNotEmpty(id)){
            setResult(workflowConfigService.get(Long.valueOf(id)));    
            }
            return SUCCESS;
        }
        
        public String delete(){
            try
            {
                String ids[]=getRequest().getParameterValues("ids");
                Long[] longIds = String2Array.stringArrayToLongArray(ids);
                workflowConfigService.remove(longIds);
            }
            catch (Exception e)
            {
                setMsg("删除失败");
                setSuccess(false);
            }
            return SUCCESS;
        }
       
        public String save()
        {
            try
            {   
                List<WorkflowConfig> wCfgList=null;
                if(workflowConfig.getDepartmentId()==null){
                wCfgList = workflowConfigService.findCfgByCondition(workflowConfig.getCompanyId(),workflowConfig.getVariableName(),workflowConfig.getWorkflowKey());
                }else{
                wCfgList = workflowConfigService.findCfgByCondition(workflowConfig.getCompanyId(),workflowConfig.getDepartmentId(),workflowConfig.getVariableName(),workflowConfig.getWorkflowKey());
                }
                
                if(wCfgList!=null&&wCfgList.size()>0){
                    if(workflowConfig.getId()==null||(workflowConfig.getId()!=null&&!(wCfgList.get(0).getId().equals(workflowConfig.getId())))){
                        setMsg("该配置信息已经存在，请勿重复配置！");
                        setSuccess(false);
                    }else{
                        if(wCfgList.size()>0){
                            workflowConfigService.evict(wCfgList.get(0));
                        }
                        workflowConfigService.save(workflowConfig); 
                    }
                }else{
                    workflowConfigService.save(workflowConfig); 
                }

            }
            catch (Exception e)
            {
                e.printStackTrace();
                setMsg("保存失败");
                setSuccess(false);
            }
            
            return SUCCESS;
        }
        

}
